package ywh.pool
{
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;
	import ywh.event.PlayerEvent;
	import ywh.manager.Core;
	import ywh.manager.CoreState;
	import ywh.manager.Tag;
	import ywh.player.AbstractShower;
	import ywh.player.media.LngPicsShower;
	import ywh.player.media.OSMFPlayer;
	import ywh.player.media.PicsListShower;
	import ywh.player.media.StageVideoPlayer;
	import ywh.player.media.TitleTxtShower;
	import ywh.player.media.TreeShower;
	import ywh.utils.XMLUtils;
	
	/**
	 * 单例类
	 * @author 杨伟华
	 */
	public class PlayerPool
	{
		static private var _$:PlayerPool;
		private var _idToTypeNameMap:Object;
		private var _idToPlayerMap:Object;
		private var _playerToIdHash:Dictionary;
		private var _playerNeedCreate:uint;
		
		private var import_TreeShower:TreeShower;
		private var import_OSMFPlayer:OSMFPlayer;
		private var import_LngPicsShower:LngPicsShower;
		private var import_PicsListShower:PicsListShower;
		private var import_TitleTxtShower:TitleTxtShower;
		private var import_StageVideoPlayer:StageVideoPlayer;
		
		public function PlayerPool(p:PrivateClass)
		{
			if (!p || _$)
			{
				throw(new Error("PlayerPool是一个单例类，请调用PlayerPool.$"));
			}
			else
			{
				_$ = this;
			}
			initPool();
		}
		
		/* DELEGATE ywh.pool.PlayerPool */
		
		static public function createPlayers(datas:XMLList):void
		{
			$.instanceCreatePlayers(datas);
		}
		
		static public function getPlayerById(id:String):AbstractShower
		{
			return $.instanceGetPlayer(id);
		}
		
		static private function get $():PlayerPool
		{
			return _$ || new PlayerPool(new PrivateClass());
		}
		
		private function instanceCreatePlayers(datas:XMLList):void
		{
			_playerNeedCreate = datas.length();
			for each (var data:XML in datas)
			{
				creatPlayer(data);
			}
		}
		
		/**
		 * 依据Id获取Player;
		 * @param	id
		 * @return
		 */
		private function instanceGetPlayer(id:String):AbstractShower
		{
			return _idToPlayerMap[id] as AbstractShower;
		}
		
		/**
		 * 构建播放器;
		 * @param	data 播放器参数;
		 * @return
		 */
		private function creatPlayer(data:XML):void
		{
			var id:String = XMLUtils.getPropString(data, Tag._PLAYER_ID);
			var typeName:String = XMLUtils.getPropString(data, Tag._PLAYER_TYPE);
			if (_idToPlayerMap[id])
			{
				if (_idToTypeNameMap[id] == typeName)
				{
					playerInitHandle();
					return;
				}
				else
				{
					delete _playerToIdHash[_idToPlayerMap[id]];
					delete _idToPlayerMap[id];
					delete _idToTypeNameMap[id];
				}
			}
			var Type:Class = getDefinitionByName(typeName) as Class;
			var player:* = new Type();
			(player as AbstractShower).initPlayer(data);
			_idToPlayerMap[id] = player;
			_idToTypeNameMap[id] = typeName;
			_playerToIdHash[player] = id;
		}
		
		private function initPool():void
		{
			_idToTypeNameMap = {};
			_idToPlayerMap = {};
			_playerToIdHash = new Dictionary(true);
			Core.addEventListener(PlayerEvent.PLAYER_INIT, playerInitHandle);
		}
		
		private function playerInitHandle(e:PlayerEvent = null):void
		{
			if (!--_playerNeedCreate)
			{
				Core.state = CoreState.PLAYERS_CREATED;
			}
		}
	}
}

class PrivateClass
{
}